//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdSimpleExtOUStorageEngine.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Processes/ExtendedOrnsteinUhlenbeckProcess.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Experimental::Processes;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (Cephei::QL::Experimental::Processes::IExtendedOrnsteinUhlenbeckProcess^ p, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
    CExtendedOrnsteinUhlenbeckProcess^ _Cp;
    CYieldTermStructure^ _CrTS;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdSimpleExtOUStorageEngine = NULL;
#endif
        _Cp = safe_cast<CExtendedOrnsteinUhlenbeckProcess^> (p);
        _Cp->Lock();
        boost::shared_ptr<QuantLib::ExtendedOrnsteinUhlenbeckProcess>& _p = static_cast<boost::shared_ptr<QuantLib::ExtendedOrnsteinUhlenbeckProcess>&> (_Cp->GetShared ()); 
        _CrTS = safe_cast<CYieldTermStructure^> (rTS);
        _CrTS->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _rTS = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_CrTS->GetShared ()); 
        QuantLib::Size _tGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (tGrid) ? (QuantLib::Size)ValueHelper::Convert (tGrid->Value) : 50); //4
        QuantLib::Size _xGrid = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (xGrid) ? (QuantLib::Size)ValueHelper::Convert (xGrid->Value) : 100); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Douglas()); //1
        _ppFdSimpleExtOUStorageEngine = new boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine> (new QuantLib::FdSimpleExtOUStorageEngine ( _p,  _rTS,  _tGrid,  _xGrid,  _schemeDesc ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdSimpleExtOUStorageEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp != nullptr) _Cp->Unlock();
        if (_CrTS != nullptr) _CrTS->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine>& childNative, Object^ owner) : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
#ifdef HANDLE
	_phFdSimpleExtOUStorageEngine = NULL;
#endif
	_ppFdSimpleExtOUStorageEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdSimpleExtOUStorageEngine));
}
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (QuantLib::FdSimpleExtOUStorageEngine& childNative, Object^ owner) : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
#ifdef HANDLE
	_phFdSimpleExtOUStorageEngine = NULL;
#endif
	_ppFdSimpleExtOUStorageEngine = new boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdSimpleExtOUStorageEngine));
    _FdSimpleExtOUStorageEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (CFdSimpleExtOUStorageEngine^ copy) : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
#ifdef HANDLE
	_phFdSimpleExtOUStorageEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdSimpleExtOUStorageEngine = new boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdSimpleExtOUStorageEngine));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (PLATFORM::Type^ t) : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
#ifdef HANDLE
	_phFdSimpleExtOUStorageEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFdSimpleExtOUStorageEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (QuantLib::Handle<QuantLib::FdSimpleExtOUStorageEngine>& childNative, Object^ owner)  : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
	_phFdSimpleExtOUStorageEngine = &childNative;
	_ppFdSimpleExtOUStorageEngine = &static_cast<boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdSimpleExtOUStorageEngine));
    _FdSimpleExtOUStorageEngineOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (QuantLib::Handle<QuantLib::FdSimpleExtOUStorageEngine> childNative)  : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
	_phFdSimpleExtOUStorageEngine = &childNative;
	_ppFdSimpleExtOUStorageEngine = &static_cast<boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdSimpleExtOUStorageEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::CFdSimpleExtOUStorageEngine (QuantLib::FdSimpleExtOUStorageEngine childNative)  : CPricingEngine(CFdSimpleExtOUStorageEngine::typeid)
{
#ifdef HANDLE
	_phFdSimpleExtOUStorageEngine = NULL;
#endif
	_ppFdSimpleExtOUStorageEngine = new boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine> (new QuantLib::FdSimpleExtOUStorageEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppFdSimpleExtOUStorageEngine));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::~CFdSimpleExtOUStorageEngine ()
{
    if (_ppFdSimpleExtOUStorageEngine != NULL)
    {
	    delete _ppFdSimpleExtOUStorageEngine;
        _ppFdSimpleExtOUStorageEngine = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::!CFdSimpleExtOUStorageEngine ()
{
    if (_ppFdSimpleExtOUStorageEngine != NULL)
    {
	    delete _ppFdSimpleExtOUStorageEngine;
    }
}
QuantLib::FdSimpleExtOUStorageEngine& Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::GetReference ()
{
    if (_ppFdSimpleExtOUStorageEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppFdSimpleExtOUStorageEngine;
}
boost::shared_ptr<QuantLib::FdSimpleExtOUStorageEngine>& Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::GetShared ()
{
    if (_ppFdSimpleExtOUStorageEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppFdSimpleExtOUStorageEngine;
}
QuantLib::FdSimpleExtOUStorageEngine* Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::GetPointer ()
{
    if (_ppFdSimpleExtOUStorageEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdSimpleExtOUStorageEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdSimpleExtOUStorageEngine>& Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::GetHandle ()
{
	if (_phFdSimpleExtOUStorageEngine == NULL)
	{
		_phFdSimpleExtOUStorageEngine = new Handle<QuantLib::FdSimpleExtOUStorageEngine> (*_ppFdSimpleExtOUStorageEngine);
	}
	return *_phFdSimpleExtOUStorageEngine;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::HasNative () 
{
	return (_ppFdSimpleExtOUStorageEngine != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdSimpleExtOUStorageEngine^ Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine::Calculate::get ()
{
    try
    {
    	(*_ppFdSimpleExtOUStorageEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdSimpleExtOUStorageEngine^ Cephei::QL::Experimental::Finitedifferences::CFdSimpleExtOUStorageEngine_Factory::Create (Cephei::QL::Experimental::Processes::IExtendedOrnsteinUhlenbeckProcess^ p, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Microsoft::FSharp::Core::FSharpOption<UInt64>^ tGrid, Microsoft::FSharp::Core::FSharpOption<UInt64>^ xGrid, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFdSimpleExtOUStorageEngine ( p,  rTS,  tGrid,  xGrid,  schemeDesc);
}
